﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WINBASE.Controls;

namespace WINBASE
{
    public partial class frmFmcf : Form
    {
        WLCDataGridView dgvCurrent;
        public BindingList<KeyValuePair<string, string>> lAllCol;
        public BindingList<KeyValuePair<string, string>> lCurCol;

        #region Constructor & Load

        public frmFmcf()
        {
            InitializeComponent();
        } 

        #endregion

        #region Event

        private void frmFmcs_Shown(object sender, EventArgs e)
        {
            InitializeData();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (lAllCol != null || lCurCol != null)
            {
                for (int i = 0; i < lCurCol.Count; i++)
                {
                    dgvCurrent.Columns[lCurCol[i].Key].Visible = true;
                    dgvCurrent.Columns[lCurCol[i].Key].DisplayIndex = i;
                }
                for (int i = 0; i < lAllCol.Count; i++)
                {
                    dgvCurrent.Columns[lAllCol[i].Key].Visible = false;
                }

                WLCMainForm myForm = (WLCMainForm)ApplicationObject.ActiveForm;
                myForm.DataGrid.SaveColumnOrder();
                if (myForm.DataGridViewFilter != null) myForm.DataGridViewFilter.InitColumn();
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnMoveToView_Click(object sender, EventArgs e)
        {
            MoveSelectedItem(lstAllCol, lstCurCol);
        }

        private void btnMoveToAll_Click(object sender, EventArgs e)
        {
            MoveSelectedItem(lstCurCol, lstAllCol);
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            MoveIndex(lstCurCol.SelectedIndex - 1);
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            MoveIndex(lstCurCol.SelectedIndex + 1);
        }

        private void btnMtp_Click(object sender, EventArgs e)
        {
            MoveIndex(0);
        }

        private void btnMtb_Click(object sender, EventArgs e)
        {
            MoveIndex(lstCurCol.Items.Count - 1);
        }

        private void btnMTAall_Click(object sender, EventArgs e)
        {
            MoveAllItem(lstCurCol, lstAllCol);
        }

        private void btnMTVall_Click(object sender, EventArgs e)
        {
            MoveAllItem(lstAllCol, lstCurCol);
        } 

        #endregion

        #region Method

        public void InitializeData()
        {

            WLCMainForm myForm = (WLCMainForm)ApplicationObject.ActiveForm;
            if (myForm == null || myForm.DataGrid == null) return;
            dgvCurrent = myForm.DataGrid;
            lAllCol = new BindingList<KeyValuePair<string, string>>();
            lCurCol = new BindingList<KeyValuePair<string, string>>();

            //lCurCol = WLCGlobalObject.FrmCurrent.ViewFilter.Column1;

            DataGridViewColumn dgvCurCol = dgvCurrent.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
            if (dgvCurCol != null)
            {
                lCurCol.Add(new KeyValuePair<string, string>(dgvCurCol.Name, dgvCurCol.HeaderText));
                while (dgvCurrent.Columns.GetNextColumn(dgvCurCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None) != null)
                {
                    DataGridViewColumn myCol = dgvCurrent.Columns.GetNextColumn(dgvCurCol, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
                    lCurCol.Add(new KeyValuePair<string, string>(myCol.Name, myCol.HeaderText));
                    dgvCurCol = myCol;
                }
            }

            foreach (DataGridViewColumn dgvCol in dgvCurrent.Columns)
            {
                if (!dgvCol.Visible)
                    lAllCol.Add(new KeyValuePair<string, string>(dgvCol.Name, dgvCol.HeaderText));
            }

            lstCurCol.DataSource = lCurCol;
            lstCurCol.ValueMember = "Key";
            lstCurCol.DisplayMember = "Value";

            lstAllCol.DataSource = lAllCol;
            lstAllCol.ValueMember = "Key";
            lstAllCol.DisplayMember = "Value";
        }

        public void MoveAllItem(ListBox FromList, ListBox ToList)
        {
            BindingList<KeyValuePair<string, string>> lFromcol = (BindingList<KeyValuePair<string, string>>)FromList.DataSource;
            BindingList<KeyValuePair<string, string>> lTocol = (BindingList<KeyValuePair<string, string>>)ToList.DataSource;
            if (lAllCol != null || lCurCol != null)
            {
                foreach (KeyValuePair<string, string> lcol in lFromcol)
                {
                    lTocol.Add(lcol);
                }
                lFromcol.Clear();

                FromList.SelectedIndex = -1;
                ToList.SelectedIndex = 0;
            }
        }

        public void MoveSelectedItem(ListBox FromList, ListBox ToList)
        {
            if (FromList.SelectedIndex >= 0)
            {
                BindingList<KeyValuePair<string, string>> lFrom = (BindingList<KeyValuePair<string, string>>)FromList.DataSource ?? new BindingList<KeyValuePair<string, string>>();
                BindingList<KeyValuePair<string, string>> lTo = (BindingList<KeyValuePair<string, string>>)ToList.DataSource ?? new BindingList<KeyValuePair<string, string>>();

                lTo.Add(lFrom[FromList.SelectedIndex]);
                int curIndex = FromList.SelectedIndex;
                lFrom.RemoveAt(curIndex);

                if (lFrom.Count > 0)
                {
                    try { FromList.SelectedIndex = curIndex; }
                    catch { FromList.SelectedIndex = curIndex - 1; }
                }
            }
        }

        public void MoveIndex(int Index)
        {
            if (lstCurCol.SelectedIndex >= 0 && (Index > -1 && Index < lstCurCol.Items.Count))
            {
                BindingList<KeyValuePair<string, string>> lFrom = (BindingList<KeyValuePair<string, string>>)lstCurCol.DataSource;
                KeyValuePair<string, string> tmpItem = lFrom[lstCurCol.SelectedIndex];

                lFrom.RemoveAt(lstCurCol.SelectedIndex);
                lFrom.Insert(Index, tmpItem);
                lstCurCol.SelectedIndex = lstCurCol.Items.IndexOf(tmpItem);
            }
        } 

        #endregion
    }
}